package com.yicong.ants.ui.circle.home;

import android.util.Pair;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.cchao.simplelib.ui.web.BackAble;
import com.yicong.ants.R;
import com.yicong.ants.databinding.CircleTabFragmentBinding;
import com.yicong.ants.ui.circle.ad.AdListFragment;
import com.yicong.ants.ui.circle.article.ArticleListFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * 社区 tab
 *
 * @author cchao
 * @version 2019-07-31.
 */
public class CircleTabFragment extends SimpleLazyFragment<CircleTabFragmentBinding> implements BackAble {
    List<Pair<String, Fragment>> mFragments = new ArrayList<>();
    FragmentPagerAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.circle_tab_fragment;
    }

    @Override
    protected void initEventAndData() {
        initViewPager();
    }

    @Override
    public void onFirstUserVisible() {

    }

    private void initViewPager() {
        mFragments.add(Pair.create("蚁丛课堂", new TutorialIndexFragment()));
        mFragments.add(Pair.create("蚁丛资讯", ArticleListFragment.get("0")));
//        mFragments.add(Pair.create("福利广告", new AdListFragment()));
        mFragments.add(Pair.create("社区广告", new AdListFragment()));
        mFragments.add(Pair.create("我的信息", new MyCircleFragment()));
        mDataBind.viewPage.setAdapter(mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i).second;
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mFragments.get(position).first;
            }
        });
        mDataBind.tabLayout.setViewPager(mDataBind.viewPage);
        mDataBind.viewPage.setOffscreenPageLimit(mFragments.size());
        mDataBind.tabLayout.onPageSelected(0);
    }

    @Override
    protected void onLoadData() {
    }

    @Override
    public boolean onBackPressed() {
        for (int i = 0; i < mFragments.size(); i++) {
            Fragment fragment = mFragments.get(i).second;
            if (fragment instanceof BackAble) {
                if (((BackAble) fragment).onBackPressed()) {
                    return true;
                }
            }
        }
        return false;
    }
}
